package cc.orange.utils.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import cc.orange.R;
import cc.orange.entity.NoteExtrEntity;
import cc.orange.utils.CircleImageView;

/**设置账号
 */

public class PopupShakeInfo {

    private static View inflate;
    private static PopupWindow popupWindow;
    public static void showPopupwindow(final Context context, NoteExtrEntity baseEntity,
                                       View view,PopuStatus pp) {//回调

        if (popupWindow == null && inflate == null) {
            inflate = LayoutInflater.from(context).inflate(R.layout.item_shakeinfo_popup, null);
            popupWindow = new PopupWindow(inflate, RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        }
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        //设置位
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        backgroundAlpha(0.3f, (Activity) context);

//        popupWindow.showAsDropDown(view);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f,(Activity) context);
            }
        });

        ImageView item_disc_rel2 = inflate.findViewById(R.id.item_disc_rel2);

        TextView item_disc_popup_text2 = inflate.findViewById(R.id.item_disc_popup_text2);//回复TA
        TextView item_disc_rel1_text2 = inflate.findViewById(R.id.item_disc_rel1_text2);//用户名
        TextView item_disc_rel1_text3 = inflate.findViewById(R.id.item_disc_rel1_text3);//内容

        CircleImageView item_shinfo_img = inflate.findViewById(R.id.item_shinfo_img);//头像

        Glide.with(context).load(baseEntity.getData().getSex() == 1?
                R.drawable.icon_login_img3 : R.drawable.icon_login_img4).into(item_shinfo_img);
        item_disc_rel1_text2.setText(baseEntity.getData().getRemarkName() == null ?
                baseEntity.getData().getName() : baseEntity.getData().getRemarkName());
        item_disc_rel1_text3.setText(baseEntity.getData().getNoteContent());

        //确认
        item_disc_popup_text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               pp.popupDismissCancle(baseEntity.getData().getNoteId());
                popupWindow.dismiss();
            }
        });

        item_disc_rel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        pp.popupShow();
    }

    public static void backgroundAlpha(float bgAlpha, Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }


    public interface PopuStatus {
        void popupShow();
        void popupDismissCancle(String se1);
    }
}
